package net.huashitong.supervision.task.service;

import com.sirdc.modules.sys.entity.SysUser;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;
import net.huashitong.supervision.task.entity.*;
import net.huashitong.supervision.task.util.HolidayUtils;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import static net.huashitong.util.UtilsTools.replaceString;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/30
 * Time: 9:57
 * To change this template use File | Settings | File Templates.
 * Description:手机短信服务
 */
@Service
public class SMSUtilService {
    @Autowired
    private SMSLogService smsLogService;
    /**
     * 新建督查任务短信提醒
     *
     * @param
     */
    public  void taskNewSmsService(TaskUnit taskUnit, SysUser user) {
        String phone = user.getMobile();
        String taskSubName = replaceString(taskUnit.getTaskSubName());
        String taskMainName = replaceString(taskUnit.getTaskMainName());
        String message = ": " + taskMainName + "，<" + taskSubName + ">已派发至贵单位";
        String msg = "【督查管理系统】您好,督查任务" + message + ",请及时登录系统进行处理";
        YunpianClient client = new YunpianClient(UtilsTools.msgapiKey).init();
        //修改账号api
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.TEXT, msg);
        param.put(YunpianClient.MOBILE, phone);
        Result<SmsSingleSend> result = client.sms().single_send(param);
        System.out.println(result.getMsg());
        SMSLogEntity smsLogEntity = new SMSLogEntity();
        smsLogEntity.setName(user.getName());
        smsLogEntity.setContent(msg);
        smsLogEntity.setSendDate(HolidayUtils.sdf1.format(new Date()));
        smsLogEntity.setSendTime(HolidayUtils.sdf2.format(new Date()));
        smsLogEntity.setType("1");//立项
        if (result.getCode()==0){
            smsLogEntity.setState("1");//发送成功
        }else {
            smsLogEntity.setState("0");//失败
            smsLogEntity.setMessage(result.getMsg());//失败原因
        }
        smsLogService.save(smsLogEntity);
        client.close();
    }

    public  void taskMainSms(TaskMain taskMain, SysUser user) {
        String phone = user.getMobile();
        String taskMainName = replaceString(taskMain.getMainName());
        String message = "<" + taskMainName + ">已派发至贵单位";
        String msg = "【督查管理系统】您好,督查任务" + message + ",请及时登录系统进行处理";
        YunpianClient client = new YunpianClient(UtilsTools.msgapiKey).init();
        //修改账号api
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.TEXT, msg);
        param.put(YunpianClient.MOBILE, phone);
        Result<SmsSingleSend> result = client.sms().single_send(param);
        System.out.println(result.getMsg());
        SMSLogEntity smsLogEntity = new SMSLogEntity();
        smsLogEntity.setName(user.getName());
        smsLogEntity.setContent(msg);
        smsLogEntity.setSendDate(HolidayUtils.sdf1.format(new Date()));
        smsLogEntity.setSendTime(HolidayUtils.sdf2.format(new Date()));
        smsLogEntity.setType("1");//立项
        if (result.getCode()==0){
            smsLogEntity.setState("1");//发送成功
        }else {
            smsLogEntity.setState("0");//失败
            smsLogEntity.setMessage(result.getMsg());//失败原因
        }
        smsLogService.save(smsLogEntity);
        client.close();
    }

    /**
     * 催办督查任务短信提醒
     */
    public  void taskHastenMessage(TaskUnit taskUnit, HastenMessage hastenMessage, SysUser user) {
        String phone = user.getMobile();

        String taskSubName = replaceString(taskUnit.getTaskSubName());
        String taskMainName = replaceString(taskUnit.getTaskMainName());
        String content = replaceString(hastenMessage.getContent());
        String message = "<" + taskMainName + ">，子事项<" + taskSubName + ">有一条新的催办信息：" + content;
        String msg = "【督查管理系统】您好,督查任务" + message + ",请及时登录系统进行处理";
        YunpianClient client = new YunpianClient(UtilsTools.msgapiKey).init();
        //修改账号api
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.TEXT, msg);
        param.put(YunpianClient.MOBILE, phone);
        Result<SmsSingleSend> result = client.sms().single_send(param);
        System.out.println(result.getMsg());
        SMSLogEntity smsLogEntity = new SMSLogEntity();
        smsLogEntity.setName(user.getName());
        smsLogEntity.setContent(msg);
        smsLogEntity.setSendDate(HolidayUtils.sdf1.format(new Date()));
        smsLogEntity.setSendTime(HolidayUtils.sdf2.format(new Date()));
        smsLogEntity.setType("3");//催办
        if (result.getCode()==0){
            smsLogEntity.setState("1");//发送成功
        }else {
            smsLogEntity.setState("0");//失败
            smsLogEntity.setMessage(result.getMsg());//失败原因
        }
        smsLogService.save(smsLogEntity);
        client.close();
    }

    /**
     * 督查即将逾期前一天提醒单位
     */
    public  void departWarning(TaskUnit taskUnit, SysUser user) {
        String phone = user.getMobile();
        String message = "";
        String taskSubName = replaceString(taskUnit.getTaskSubName());
        String taskMainName = replaceString(taskUnit.getTaskMainName());
        if ("1".equals(taskUnit.getUnitType())) {
            message = "<" + taskMainName + ">，子事项<" + taskSubName + ">,牵头单位：" + taskUnit.getDeptName() + "  还有一天超期";
        } else {
            message = "<" + taskMainName + ">，子事项<" + taskSubName + ">,协办单位：" + taskUnit.getDeptName() + "  还有一天超期";
        }
        String msg = "【督查管理系统】您好,督查任务" + message + ",请及时登录系统进行处理";
        YunpianClient client = new YunpianClient(UtilsTools.msgapiKey).init();
        //修改账号API
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        param.put(YunpianClient.TEXT, msg);
        Result<SmsSingleSend> result = client.sms().single_send(param);
        System.out.println(result.getMsg());
        SMSLogEntity smsLogEntity = new SMSLogEntity();
        smsLogEntity.setName(user.getName());
        smsLogEntity.setContent(msg);
        smsLogEntity.setSendDate(HolidayUtils.sdf1.format(new Date()));
        smsLogEntity.setSendTime(HolidayUtils.sdf2.format(new Date()));
        smsLogEntity.setType("5");//提醒
        if (result.getCode()==0){
            smsLogEntity.setState("1");//发送成功
        }else {
            smsLogEntity.setState("0");//失败
            smsLogEntity.setMessage(result.getMsg());//失败原因
        }
        smsLogService.save(smsLogEntity);
        client.close();
    }

    @Autowired
    private SpecialTaskService specialTaskService;
    public void departWarning(SpecialTask specialTask,SysUser user ) {
        String phone = user.getMobile();
        String message = "";
        String taskMainName = replaceString(specialTask.getTaskMainName());
            message = "<" + taskMainName + ">，  还有一天超期";
        String msg = "【督查管理系统】您好,督查任务: 单位重大事项" + message + ",请及时登录系统进行处理";
        YunpianClient client = new YunpianClient(UtilsTools.msgapiKey).init();
        //修改账号API
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        param.put(YunpianClient.TEXT, msg);
        Result<SmsSingleSend> result = client.sms().single_send(param);
        System.out.println(result.getMsg());
        SMSLogEntity smsLogEntity = new SMSLogEntity();
        smsLogEntity.setName(user.getName());
        smsLogEntity.setContent(msg);
        smsLogEntity.setSendDate(HolidayUtils.sdf1.format(new Date()));
        smsLogEntity.setSendTime(HolidayUtils.sdf2.format(new Date()));
        smsLogEntity.setType("5");//提醒
        if (result.getCode()==0){
            smsLogEntity.setState("1");//发送成功
        }else {
            smsLogEntity.setState("0");//失败
            smsLogEntity.setMessage(result.getMsg());//失败原因
        }
        smsLogService.save(smsLogEntity);
        client.close();
    }
public void departOverdue(SpecialTask specialTask,SysUser user ) {
        String phone = user.getMobile();
        String message = "";
        String taskMainName = replaceString(specialTask.getTaskMainName());
            message = "<" + taskMainName + ">，  超期未完成";
        String msg = "【督查管理系统】您好,督查任务: 单位重大事项" + message + ",请及时登录系统进行处理";
        YunpianClient client = new YunpianClient(UtilsTools.msgapiKey).init();
        //修改账号API
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        param.put(YunpianClient.TEXT, msg);
        Result<SmsSingleSend> result = client.sms().single_send(param);
        System.out.println(result.getMsg());
        SMSLogEntity smsLogEntity = new SMSLogEntity();
        smsLogEntity.setName(user.getName());
        smsLogEntity.setContent(msg);
        smsLogEntity.setSendDate(HolidayUtils.sdf1.format(new Date()));
        smsLogEntity.setSendTime(HolidayUtils.sdf2.format(new Date()));
        smsLogEntity.setType("5");//提醒
        if (result.getCode()==0){
            smsLogEntity.setState("1");//发送成功
        }else {
            smsLogEntity.setState("0");//失败
            smsLogEntity.setMessage(result.getMsg());//失败原因
        }
        smsLogService.save(smsLogEntity);
        client.close();
    }

    /**
     * 督查任务超期提醒单位
     */
    public  void departOverdue(TaskUnit taskUnit, SysUser user) {
        String phone = user.getMobile();
        String message = "";
        String taskSubName = replaceString(taskUnit.getTaskSubName());
        String taskMainName = replaceString(taskUnit.getTaskMainName());
        if ("1".equals(taskUnit.getUnitType())) {
            message = "<" + taskMainName + ">，子事项<" + taskSubName + ">,牵头单位：" + taskUnit.getDeptName() + "  超期未完成";
        } else {
            message = "<" + taskMainName + ">，子事项<" + taskSubName + ">,协办单位：" + taskUnit.getDeptName() + "  超期未完成";

        }
        String msg = "【督查管理系统】您好,督查任务" + message + ",请及时登录系统进行处理";
        YunpianClient client = new YunpianClient(UtilsTools.msgapiKey).init();
        //修改账号API
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        param.put(YunpianClient.TEXT, msg);
        Result<SmsSingleSend> result = client.sms().single_send(param);
        System.out.println(result.getMsg());
        SMSLogEntity smsLogEntity = new SMSLogEntity();
        smsLogEntity.setName(user.getName());
        smsLogEntity.setContent(msg);
        smsLogEntity.setSendDate(HolidayUtils.sdf1.format(new Date()));
        smsLogEntity.setSendTime(HolidayUtils.sdf2.format(new Date()));
        smsLogEntity.setType("4");//超期
        if (result.getCode()==0){
            smsLogEntity.setState("1");//发送成功
        }else {
            smsLogEntity.setState("0");//失败
            smsLogEntity.setMessage(result.getMsg());//失败原因
        }
        smsLogService.save(smsLogEntity);
        client.close();
    }

    /**
     * 单位任务批转短信
     *
     * @param taskUnit
     */
    public  void taskUserSMS(TaskUnit taskUnit, SysUser user) {
        String taskSubName = replaceString(taskUnit.getTaskSubName());
        String taskMainName = replaceString(taskUnit.getTaskMainName());
        String phone = user.getMobile();
        String message = "<" + taskMainName + ">，子事项<" + taskSubName + ">已批转至您";
        String msg = "【督查管理系统】您好,督查任务" + message + ",请及时登录系统进行处理";
        YunpianClient client = new YunpianClient(UtilsTools.msgapiKey).init();
        //修改账号API
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.MOBILE, phone);
        param.put(YunpianClient.TEXT, msg);
        Result<SmsSingleSend> result = client.sms().single_send(param);
        System.out.println(result.getMsg());
        SMSLogEntity smsLogEntity = new SMSLogEntity();
        smsLogEntity.setName(user.getName());
        smsLogEntity.setContent(msg);
        smsLogEntity.setSendDate(HolidayUtils.sdf1.format(new Date()));
        smsLogEntity.setSendTime(HolidayUtils.sdf2.format(new Date()));
        smsLogEntity.setType("2");//批转
        if (result.getCode()==0){
            smsLogEntity.setState("1");//发送成功
        }else {
            smsLogEntity.setState("0");//失败
            smsLogEntity.setMessage(result.getMsg());//失败原因
        }
        smsLogService.save(smsLogEntity);
        client.close();
    }
}
